package org.blogsite.greencode.struts2.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.blogsite.greencode.bean.InstructorManage_Instructor;
import org.blogsite.greencode.service.InstructorService;
import org.blogsite.greencode.service.SchoolingManageService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author：wzj
 * E-mail: jsun789@gmail.com
 * @date：2010-8-1 下午04:05:25
 */
public class InstructorManageAction extends ActionSupport {
	/*
	 * InstructorService对象,由spring容器注入
	 */
	private InstructorService instructorService;
	/*
	 * instructor bean对象,由spring容器注入
	 */
	private InstructorManage_Instructor instructor;
	HttpServletRequest request;
	private String searchText;
	private String searchRadio;
	
	//显示录入讲师信息
	public String displaySave() throws Exception {
		try {
			request=ServletActionContext.getRequest();
			request.setAttribute("add", "1");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "SUCCESS_DISPLAY";
	}
	// 显示讲师信息查询
	public String displaySearch() throws Exception {

		request = ServletActionContext.getRequest();
		request.setAttribute("search", "1");
		request.setAttribute("searchtitle", "1");
		return "SUCCESS_DISPLAY";
	}
	// 显示修改讲师信息
	public String displayModify() throws Exception {
		
		request = ServletActionContext.getRequest();
		request.setAttribute("id", request.getParameter("id"));
		request.setAttribute("name", request.getParameter("name"));
		request.setAttribute("sex", request.getParameter("sex"));
		request.setAttribute("age", request.getParameter("age"));
		request.setAttribute("email", request.getParameter("email"));
		request.setAttribute("job_num", request.getParameter("job_num"));
		request.setAttribute("post", request.getParameter("post"));
		request.setAttribute("modify", "1");
		return "SUCCESS_DISPLAY";
	}
	
	//录入讲师信息
	public String save() throws Exception {
		request=ServletActionContext.getRequest();
		System.out.println("display save");
		if("".equals(instructor.getName())||instructor.getName()==null){
			
			request.setAttribute("add", "1");
			request.setAttribute("failuresave_instructornull","1");
			return "FAILURE_SAVE";
		}
		if("".equals(instructor.getJob_num())||instructor.getJob_num()==null){
			
			request.setAttribute("add", "1");
			request.setAttribute("failuresave_instructornumnull","1");
			return "FAILURE_SAVE";
		}
		List list=instructorService.byIdSearch(instructor.getJob_num());
		if(list!=null){
			request.setAttribute("add", "1");
			request.setAttribute("failuresave_instructornumexist","1");
			return "FAILURE_SAVE";
		}
			
		instructorService.save(instructor);
		/*
		 * 调用服务层的InstructorService,该对象由spring容器注入
		 */
		request.setAttribute("add", "1");
		request.setAttribute("savesuccess", "1");
		return "SUCCESS_SAVE";
	}
	
	// 查询讲师信息
	public String search() throws Exception {
		List list;
		HttpServletRequest hs = ServletActionContext.getRequest();
		if (getSearchRadio().equals("all")||"".equals(getSearchRadio())||getSearchRadio()==null) {
			// 调用服务层,获取所有讲师信息的集合
			list = instructorService.allSearch();
			hs.setAttribute("allInstructor", list);
			if (list.iterator().hasNext() == false) {
				hs.setAttribute("searchnodata", "1");
			} else
				hs.setAttribute("searchhavedata", "1");

		} else if (getSearchRadio().equals("no")) {
			// 调用服务层,获取指定讲师号所有讲师信息
			try {

				list = instructorService.byIdSearch(getSearchText());
				if (list.iterator().hasNext() == false) {
					hs.setAttribute("searchnodata", "1");
				} else
					hs.setAttribute("searchhavedata", "1");
				hs.setAttribute("allInstructor", list);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (getSearchRadio().equals("name")) {
			// 调用服务层,获取指定讲师的姓名
			try {
				list =  instructorService.byNameSearch(getSearchText());
				hs.setAttribute("allInstructor", list);
				if (list.iterator().hasNext() == false) {
					hs.setAttribute("searchnodata", "1");
				} else
					hs.setAttribute("searchhavedata", "1");
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (getSearchRadio().equals("blear")) {
			// 调用服务层,获取模糊查询所有讲师的集合
			list = instructorService.byBlearSearch(getSearchText());
			hs.setAttribute("allInstructor", list);
			if (list.iterator().hasNext() == false) {
//				System.out.println("no data blear");
				hs.setAttribute("searchnodata", "1");
			} else
//				System.out.println("have data blear");
				hs.setAttribute("searchhavedata", "1");
		}
		hs.setAttribute("search", "1");
		return "SUCCESS_DISPLAY";
	}
	
	//修改讲师信息
	public String modify() throws Exception{
//		 TODO Auto-generated method stub
		request=ServletActionContext.getRequest();
		/*
		 * 调用服务层的InstructorService,该对象由spring容器注入
		 */
		instructorService.modify(instructor);
		System.out.println("sdfdscfgvrgfvf"+instructor.getAge());
		request.setAttribute("modifysuccess", "1");
		return "SUCCESS_MODIFY";
	}
	
	//删除讲师信息
	@SkipValidation 
	public String delete() throws Exception{
		request=ServletActionContext.getRequest();
		instructorService.delete(new InstructorManage_Instructor(Integer.parseInt(request.getParameter("id"))));
		
		request.setAttribute("deletesuccess", "1");
		return "SUCCESS_DELETE";
	}
	/*显示删除讲师*/
	public String displayDelete() throws Exception {
		request = ServletActionContext.getRequest();
		request.setAttribute("search", "1");
		request.setAttribute("delete","1");
		return "SUCCESS_DISPLAY";
	}
	/*修改之前,先查询*/
	public String beforeModify() throws Exception {
		request = ServletActionContext.getRequest();
		request.setAttribute("search", "1");
		request.setAttribute("beforemodify","1");
		return "SUCCESS_DISPLAY";
	}
	
	
//	//查询讲师信息
//	@SkipValidation 
//	public String search1() throws Exception {
//		List search=instructorService.allSearch();
//		/*
//		 * 调用服务层的InstructorService,该对象由spring容器注入
//		 */
//		request=ServletActionContext.getRequest();
//		request.setAttribute("search",search);
//		
//		return "success_search";
//	}
	
//	//查询单个讲师信息
//	@SkipValidation 
//	public String searchById() throws Exception{
////		 TODO Auto-generated method stub
//		request=ServletActionContext.getRequest();
//		String id=request.getParameter("id");
//		int i_id=Integer.parseInt(id);
//		instructor=instructorService.byIdSearch(i_id);
//		/*
//		 * 调用服务层的InstructorService,该对象由spring容器注入
//		 */
//		request.setAttribute("instructor",instructor);
//		System.out.println("ddddd"+instructor.getName());
//		return "su_searchbyid";
//	}
	
	
	
	
	public InstructorService getInstructorService() {
		return instructorService;
	}
	public void setInstructorService(InstructorService instructorService) {
		this.instructorService = instructorService;
	}
	public InstructorManage_Instructor getInstructor() {
		return instructor;
	}
	public void setInstructor(InstructorManage_Instructor instructor) {
		this.instructor = instructor;
	}
	public String getSearchText() {
		return searchText;
	}
	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}
	public String getSearchRadio() {
		return searchRadio;
	}
	public void setSearchRadio(String searchRadio) {
		this.searchRadio = searchRadio;
	}
	
	
	
	

}
